//
//  NSFileManager+ZXDiskSpace.m
//  ZXCategoriesUtil
//
//  Created by zhuxm on 2024/9/3.
//

#import "NSFileManager+ZXDiskSpace.h"

@implementation NSFileManager (ZXDiskSpace)


#pragma mark - 磁盘空间
/*
attrs    __NSDictionaryI *    5 key/value pairs    0x0000600002c0fa80
[0]    (null)    @"NSFileSystemNodes" : (long)8061753010
[1]    (null)    @"NSFileSystemSize" : (long)994662584320
[2]    (null)    @"NSFileSystemFreeSize" : (long)825227214848
[3]    (null)    @"NSFileSystemFreeNodes" : (long)8058859520
[4]    (null)    @"NSFileSystemNumber" : (long)16777233
 */
// 获取磁盘总空间
+ (int64_t)zx_diskSpace
{
    NSError *error = nil;
    NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:&error];
    if (error) return -1;
    NSNumber *totalSize = [attrs objectForKey:NSFileSystemSize];
    int64_t space =  [totalSize longLongValue];
    if (space < 0) space = -1;
    return space;
}
+ (CGFloat)zx_diskSpaceGB{
    int64_t space = [self zx_diskSpace];
    CGFloat kb = space/1024.0;
    CGFloat mb = kb/1024.0;
    CGFloat gb = mb/1024.0;
    return gb;
}

// 获取未使用的磁盘空间
+ (int64_t)zx_diskFreeSpace
{
    NSError *error = nil;
    NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfFileSystemForPath:NSHomeDirectory() error:&error];
    if (error) return -1;
    int64_t space =  [[attrs objectForKey:NSFileSystemFreeSize] longLongValue];
    if (space < 0) space = -1;
    return space;
}
+ (CGFloat)zx_diskFreeSpaceGB{
    int64_t space = [self zx_diskFreeSpace];
    CGFloat kb = space/1024.0;
    CGFloat mb = kb/1024.0;
    CGFloat gb = mb/1024.0;
    return gb;
}

// 获取已使用的磁盘空间
+ (int64_t)zx_diskUsedSpace
{
    int64_t totalDisk = [self zx_diskSpace];
    int64_t freeDisk = [self zx_diskFreeSpace];
    if (totalDisk < 0 || freeDisk < 0) return -1;
    int64_t usedDisk = totalDisk - freeDisk;
    if (usedDisk < 0) usedDisk = -1;
    return usedDisk;
}

+ (CGFloat)zx_diskUsedSpaceGB{
    int64_t space = [self zx_diskUsedSpace];
    CGFloat kb = space/1024.0;
    CGFloat mb = kb/1024.0;
    CGFloat gb = mb/1024.0;
    return gb;
}
@end
